package sendMessage;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class ReceiveDemo {
    public static void main(String[] args) throws IOException {
        //接受数据端
        //1.创建数据接受对象
        //2.接受数据包
        //3.解析数据
        //4.打印数据
        DatagramSocket ds = new DatagramSocket(10011);

        byte[] bytes = new byte[100];
        DatagramPacket dp = new DatagramPacket(bytes, bytes.length);

        while (true) {
            ds.receive(dp);
            byte[] data = dp.getData();
            int len = dp.getLength();
            String ip = dp.getAddress().getHostAddress();
            String name = dp.getAddress().getHostName();

            System.out.println(name + ": " + (new String(data, 0, len)));
        }

    }
}
